package com.yysoft.core.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yysoft.core.web.model.AjaxResponse;
import com.yysoft.core.web.model.YYException;

public abstract class BaseController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	/**
	 * 统一异常处理，统一返回json字符串
	 * @param e
	 * @return
	 */
	@ExceptionHandler
	@ResponseBody
    public AjaxResponse handleException(Exception e)throws Exception {
		log.error("", e);
		AjaxResponse msg = new AjaxResponse();
		
		// 异常为YYException的，将message显示给前端
		if(e instanceof YYException){
        	YYException yyException = (YYException)e;
        	msg.setCode(yyException.getErrorCode());
        	msg.setMessage(yyException.getErrorMessage());
        }
        // 其他异常统一显示错误消息：操作失败！
        else{
        	msg.setCode(99);
        	msg.setMessage("操作失败！");
        }
        return msg;
    }
	
}
